package org.musicsweet.result;

import lombok.Getter;
import lombok.Setter;
import org.musicsweet.constant.StringConstant;

/**
 * 统一返回给前段的对象
 */
@Setter
@Getter
public class ResultData<T> {
    /**
     * 返回给前端的code码
     */
    private Integer code;
    /**
     * 返回给前段的信息
     */
    private String msg;
    /**
     * f返回给前段的数据
     */
    private T data;

    /**
     * 封装错误的返回信息
     * @param errorMsg
     * @return
     */
    public static ResultData createFail(String errorMsg){
        ResultData resultData=new ResultData();
        resultData.setCode(StringConstant.RS_DATA_FAIL_CODE);
        resultData.setMsg(errorMsg);
        return resultData;
    }
    /**
     * 封装成功的返回信息
     * @param errorMsg
     * @return
     */
    public static ResultData createSuccess(String errorMsg){
        ResultData resultData=new ResultData();
        resultData.setCode(StringConstant.RS_DATA_SUCCESS_CODE);
        resultData.setMsg(errorMsg);
        return resultData;
    }

    /**
     * 封装返回前段的信息并且返回数据
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResultData successData(String msg,T data){
        ResultData resultData = new ResultData();
        resultData.setCode(StringConstant.RS_DATA_SUCCESS_CODE);
        resultData.setMsg(msg);
        resultData.setData(data);
        return resultData;
    }
}
